package gameObjects;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.state.RenderState;
import com.jme.system.DisplaySystem;
import com.jme.util.Timer;

import audio.SFX;

public class UpdatePills extends Node {
	private static final long 		serialVersionUID = 2704101244L;
	private Text 			pill;
    private StringBuffer	strBuf;
    private String			strKeys;
    private int 			height = DisplaySystem.getDisplaySystem().getHeight();
    private SFX				shieldOn  = new SFX("sound/life.ogg", false, 0.2f, 0.2f, false);
    private SFX				shieldOff = new SFX("sound/tshieldoff.ogg", false, 0.2f, 0.2f, false);
    private SFX				timePass  = new SFX("sound/tick.ogg", false, 0.2f, 0.2f, false);
    private float			ratio = 1.28f;
    private float 			posX =0;
    private int 			pillTime = 15;	
    private boolean			isHuman;
    private int				oneSecond;
    
	public UpdatePills(boolean isHumanPlayer)
	{
		
		super("PILLS");
		this.isHuman=isHumanPlayer;
		this.oneSecond=0;
		posX = DisplaySystem.getDisplaySystem().getWidth() /ratio;
		
		NumberFormat formatter = new DecimalFormat("00"); 
		if(isHuman)
			strKeys = formatter.format(0);
		else
			strKeys = "";
		
		strBuf=new StringBuffer(10);
		strBuf.setLength(0);
		strBuf.append(strKeys);
		
		pill = new Text();
		pill = Text.createDefaultTextLabel( "PILL label" );
		pill.setTextColor(ColorRGBA.red.clone());
        pill.setCullHint(CullHint.Never);
        pill.setTextureCombineMode(TextureCombineMode.Replace);
		pill.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		pill.setLightCombineMode(Spatial.LightCombineMode.Off);
		pill.setLocalTranslation(new Vector3f(posX, height - 20, 0));
		pill.print(strBuf);
		pill.updateRenderState();
		
		this.setName("PILL_NODE");
        this.setRenderState(this.getRenderState(RenderState.StateType.Blend));
        this.setRenderState(this.getRenderState(RenderState.StateType.Texture));
        this.attachChild(pill);
        this.setCullHint(CullHint.Never);
        this.updateRenderState();
	}

	public boolean update()
	{
		if (pillTime==0) {shieldOff();stop();return false;}
		if(isHuman)
		{	// il giocatore umano vede quanto tempo rimane
			NumberFormat formatter = new DecimalFormat("00");
			strKeys = formatter.format(pillTime);   
			pill.setLocalTranslation(new Vector3f(posX, height-20, 0));
	        strBuf.setLength(0);
	        strBuf.append(strKeys);
	        pill.print(strKeys);
	        pill.updateRenderState();
		}
		timePass();
		return true;
	}
	
	private void shieldOn()
	{
		if (isHuman)
		{
			shieldOn.setVolume(5);
			shieldOn.Play();
		}
	}
	
	private void shieldOff()
	{
		if(isHuman)
		{
			shieldOff.setVolume(5);
			shieldOff.Play();
		}
	}
	
	private void timePass()
	{
		int nowTime = (int)Timer.getTimer().getTimeInSeconds();
		if(nowTime-oneSecond==1)
		{
			System.out.println("TIME PASS 1 SECOND");
			oneSecond=nowTime;
			timePass.Play();
			pillTime--;
		}
	}
	
	public void start() {
		pillTime=16;
		oneSecond=(int)Timer.getTimer().getTimeInSeconds();
		shieldOn();
	}
	
	public void resetTime()
	{
		shieldOn();
		pillTime=16;
		oneSecond=(int)Timer.getTimer().getTimeInSeconds();
	}
	
	public void stop()
	{
		NumberFormat formatter = new DecimalFormat("00");
		strKeys = formatter.format(0);   
		pill.setLocalTranslation(new Vector3f(posX, height-20, 0));
        strBuf.setLength(0);
        strBuf.append(strKeys);
        pill.print(strKeys);
        pill.updateRenderState();
	}

}
